package com.ficus.road.maintain.third.zh.xlj;

import com.alibaba.fastjson.JSONObject;
import com.ficus.road.maintain.core.model.zh.xlj.XljWarning;
import com.ficus.road.maintain.core.model.zh.xlj.XljWarningFlow;
import com.ficus.road.maintain.core.model.zh.xlj.XljWarningFlowZH;
import com.ficus.road.maintain.dao.xlj.XljWarningDao;
import com.ficus.road.maintain.third.zh.BaseZH;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @author xiawen yang
 * @date 2022/6/28 上午10:15
 */
@Component
@Slf4j
public class ZHXljWarningFlow extends BaseZH {
    @Value("${present.flag}")
    private Boolean present;
    @Autowired
    private XljWarningDao xljWarningDao;

    @PostConstruct
    void init() {
        arg0 = "xljWarningFlow";
        errMsg = "调用中海接口获取下立交预警流程异常!";
        successMsg = "调用中海接口获取下立交预警流程成功!";
    }

    //    @Scheduled(cron = "0/5 * * * * ?", zone = "GMT+8:00")
    @Scheduled(cron = "${xlj.sync_cron}", zone = "GMT+8:00")
    public void refreshXljWarningFlow() {
        init();
        Date monthAgo = DateUtils.addDays(new Date(), -30);
        List<XljWarning> xljWarningList = xljWarningDao.findByQuery(new Query(Criteria.where("create_time").gte(monthAgo)));
//        List<XljWarning> xljWarningList = xljWarningDao.findAll();
        xljWarningList.forEach(warning -> {
            arg1 = "{\"WARNING_ID\":\"" + warning.getId() + "\"}";
            String warningFlowStr = getZHData();
            if (StringUtils.isEmpty(warningFlowStr)) {
                log.info("获取该报警流程信息失败, 报警id: {}", warning.getId());
                return;
            }
            Date date = new Date();
            List<XljWarningFlowZH> xljWarningFlowZHList = JSONObject.parseArray(warningFlowStr, XljWarningFlowZH.class);
            if (ObjectUtils.isEmpty(xljWarningFlowZHList)) {
                return;
            }
            XljWarningFlowZH xljWarningFlowZH = xljWarningFlowZHList.get(0);
            XljWarningFlow xljWarningFlow = xljWarningFlowZH.convert2XljWarningFlow(xljWarningFlowZH, date);
            Optional<XljWarning> any = xljWarningList.stream().filter(item -> item.getId().equals(warning.getId())).findAny();
            if (any.isEmpty()) {
                return;
            }
            XljWarning xljWarning = any.get();
            xljWarning.setXljWarningFlow(xljWarningFlow);
            xljWarningDao.saveOrUpdate(xljWarning);
//            AuthenticationManager
//            UsernamePasswordAuthenticationToken
//            Authentication
//            UserDetails
        });
    }

    @Override
    public boolean printLogInfo() {
        return false;
    }
}
